package com.ansorgit.helpful.formatFavourite;

import com.ansorgit.helpful.AbstractEditorHandler;
import com.ansorgit.helpful.SearchDirection;
import com.intellij.codeInsight.actions.ReformatCodeProcessor;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.*;

/**
 * User: jansorg
 * Date: Mar 5, 2010
 * Time: 9:38:15 PM
 */
public class FormatElementHandler extends AbstractEditorHandler {
    private boolean select = true;

    public FormatElementHandler() {
        super(false, SearchDirection.Backwards);
    }

    @Override
    protected void elementAction(Project project, PsiElement psiElement, Editor editor) {
        TextRange r = psiElement.getTextRange();
        if (select) {
            editor.getSelectionModel().setSelection(r.getStartOffset(), r.getEndOffset());
        }

        new ReformatCodeProcessor(project, psiElement.getContainingFile(), r).run();
    }
}
